package com.uwlaser.cutImpl;

import com.alibaba.fastjson.JSONObject;
import com.uwlaser.cut.BaseMethodCut;
import com.uwlaser.rest.DeviceToMesController;
import com.uwlaser.service.dto.DeviceLoginParam;
import com.uwlaser.utils.MyHttpUtil;
import com.uwlaser.utils.MyTcpSocketUtil;
import com.uwlaser.utils.MyWebServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;

/** 测试Cut
 * @author LiuZhiHui
 * @version 1.0
 * @description: TODO
 * @date 2022/8/17 13:33
 */
@Slf4j
@Component
public class TestCutImpl implements BaseMethodCut {
    @Override
    public Method getMethodClass() {
        return this.getMethodClass(DeviceToMesController.class, "login", DeviceLoginParam.class);
    }

    @Override
    public void before(ProceedingJoinPoint joinPoint) throws Exception{
        //获取参数
        Object[] args = joinPoint.getArgs();
        DeviceLoginParam param = (DeviceLoginParam) args[0];

        //发送TcpSocket请求测试
        /*String res = testTcpSocketRequest(param);
        System.out.println("TcpSocket请求结果：" + res);*/


        //发送WebService请求测试
        /*String res = testWebServiceRequest(param);
        System.out.println("WebService请求结果：" + res);*/


        //发送Http请求测试
        /*String res = testHttpRequest(param);
        System.out.println("HTTP请求结果："+ res);*/

    }

    @Override
    public void after(ProceedingJoinPoint joinPoint, Object returnValue) throws Exception {
        log.info("after running........");
    }





    /**
     * 测试TcpSocket请求
     * @param param
     * @return
     */
    private String testTcpSocketRequest(DeviceLoginParam param) throws Exception {
        // 测试短链接
        String res = MyTcpSocketUtil.sendShortConnectionRequest(
                "127.0.0.1",
                8888,
                JSONObject.toJSONString(param)
        );

        // 测试长链接
        /*String res = MyTcpSocketUtil.sendLongConnectionRequest(
                "127.0.0.1",
                8888,
                JSONObject.toJSONString(param)
        );*/

        return res;
    }


    /**
     * 测试WebService请求
     * @param param
     * @return
     */
    private String testWebServiceRequest(DeviceLoginParam param) throws Exception {
        String url = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?wsdl";
        String methodName = "getEnCnTwoWayTranslator";
        String bodyJson ="{\"Word\":\""+param.getUserName()+"\"}";

        //String headerJson = "{\"test\":\"测试请求头数据\"}";
        //JSONObject jsonObject = MyWebServiceUtil.sendPostRequestWS(url, methodName, bodyJson,headerJson);

        JSONObject jsonObject = MyWebServiceUtil.sendPostRequestWS(url, methodName, bodyJson);
        return JSONObject.toJSONString(jsonObject);
    }


    /**
     * 测试Http请求
     * @param param
     * @return
     */
    private String testHttpRequest(DeviceLoginParam param) throws Exception {
        String url = "http://127.0.0.1:8001/api/deviceToMes/testPost";
        HashMap<String, Object> params = new HashMap<>();
        params.put("deviceCode",param.getDeviceCode());
        params.put("isOnline","1");
//        params.put("operator",param.getOperator());
        params.put("timestamp","2022-04-25 10:22:43");

        String post = MyHttpUtil.post(url, JSONObject.toJSONString(params));
        return post;
    }
}
